﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web.Mvc;

namespace Kaiser.FilterProviders.FilterCriterion
{
    public class ControllerTypeFilterCriteria : IFilterCriteria
    {
        private readonly List<Type> _types = new List<Type>();

        public void AddType(Type controllerType)
        {
            if(controllerType==null)
                throw new ArgumentNullException("controllerType");

            if (!typeof(IController).IsAssignableFrom(controllerType))
                throw new ArgumentOutOfRangeException("controllerType");

            _types.Add(controllerType);
        }
        public void AddType<T>() where T : IController
        {
            _types.Add(typeof(T));
        }

        #region IFilterCriteria Members

        public bool Match(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
        {
            if (controllerContext == null)
                throw new ArgumentNullException("controllerContext");
            if (actionDescriptor == null)
                throw new ArgumentNullException("actionDescriptor");

            IController currentController = controllerContext.Controller;

            if (currentController == null)
                throw new ArgumentException("The Controller is null","controllerContext");

            return _types.Contains(currentController.GetType());
        }

        #endregion
    }
}
